package com.fone.player.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.fone.player.R;
import com.fone.player.activity.personal.UserInfoActivity;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.Request;
import com.fone.player.client.Rst;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.LoginUtil;

public class ModifyPwdDialog implements OnClickListener {
	
	private static final String TAG = "ModifyPwdDialog"; 
	
	private final EditText originalPwdEt, newPwdEt, confirmPwdEt;
	private CustomDialog dialog;
	private UserInfoActivity mActivity;

	@SuppressWarnings("deprecation")
	public ModifyPwdDialog(Context mContext, UserInfoActivity activity) {
		mActivity = activity;
		View changePwdView = LayoutInflater.from(mContext).inflate(
				R.layout.personal_change_pwd_dialog, null);
		dialog = new CustomDialog((Context) activity,
				changePwdView);
		dialog.show();

		changePwdView.setMinimumWidth((int) (activity.getWindowManager()
				.getDefaultDisplay().getWidth() * 0.8));// 设置dialog的宽度

		originalPwdEt = (EditText) changePwdView
				.findViewById(R.id.dialog_origional_pwd_et);
		newPwdEt = (EditText) changePwdView
				.findViewById(R.id.dialog_new_pwd_et);
		confirmPwdEt = (EditText) changePwdView
				.findViewById(R.id.dialog_confirm_pwd_et);

		Button changeSureBtn = (Button) changePwdView
				.findViewById(R.id.change_name_sure_id);
		changeSureBtn.setOnClickListener(this);

		Button changeCancelBtn = (Button) changePwdView
				.findViewById(R.id.change_name_cancel_id);
		changeCancelBtn.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.change_name_sure_id:
			if(FoneUtil.isNetOkWithToast(mActivity.getApplication())){
				String oldData = originalPwdEt.getText().toString();
				String newData = newPwdEt.getText().toString();
				String againData = confirmPwdEt.getText().toString();
				if(checkPwdData(oldData, newData, againData)){
					mActivity.mHandler.sendEmptyMessage(UserInfoActivity.SHOW_WAITTING);
					oldData = FoneUtil.Base64Encode(FoneUtil.Md5(oldData));
					newData = FoneUtil.Base64Encode(newData);
					//请求网络 修改密码
					mActivity.mHandler.sendEmptyMessage(UserInfoActivity.SHOW_WAITTING);
					Request.getInstance().updateuser(1, oldData, newData, new changePswCallback());
					dialog.dismiss();
				}
			}
			break;
		case R.id.change_name_cancel_id:
			dialog.dismiss();
			break;

		default:
			break;
		}
	}
	
	/**
	 * 校验密码
	 * @param oldData
	 * @param newData
	 * @param againData
	 * @return
	 */
	public boolean checkPwdData(String oldData, String newData, String againData) {
		if (TextUtils.isEmpty(oldData)) {
			Toast.makeText(mActivity.getApplicationContext(), "请输入旧密码", 0).show();
			return false;
		}
		if (TextUtils.isEmpty(newData)) {
			Toast.makeText(mActivity.getApplicationContext(), "请输入新密码", 0).show();
			return false;
		}
		if (TextUtils.isEmpty(againData)) {
			Toast.makeText(mActivity.getApplicationContext(), "请再次输入新密码", 0).show();
			return false;
		}

		if (oldData.equals(newData)) {
			Toast.makeText(mActivity.getApplicationContext(), "新旧密码相同", 0).show();
			return false;
		}

		if (!LoginUtil.checkPassword(newData)) {
			Toast.makeText(mActivity.getApplicationContext(), "密码由6-24位英文数字组成", 0).show();
			return false;
		}
		if (!newData.equals(againData)) {
			Toast.makeText(mActivity.getApplicationContext(), "新密码输入不一致", 0).show();
			return false;
		}
		return true;
	}

	private class changePswCallback implements Callback<Rst>{

		@Override
		public void onSuccess(Rst t) {
			// TODO Auto-generated method stub
			L.v(TAG, "loginOutCallBack onSuccess", t.toString());
			if(t.result!=0){
				if(t.error!=null){
					L.v(TAG, "onSuccess","errorcode : " + t.error.errorcode);
					try{
						switch (Integer.valueOf(t.error.errorcode)) {
						case 111://用户名或密码错误
						case 116://用户名或密码错误
							FoneUtil.showToast(mActivity.getApplicationContext(), "原密码输入有误");
							break;
						default:
							FoneUtil.showToast(mActivity.getApplicationContext(), "修改失败");
							break;
						}
					}catch(Exception e){
						FoneUtil.showToast(mActivity.getApplicationContext(), "登录失败");
					}
					mActivity.mHandler.sendEmptyMessage(UserInfoActivity.CANCEL_WAITTING);
					return;
				}
			}
			mActivity.mHandler.sendEmptyMessage(UserInfoActivity.CHANGE_PSW_SUCCESS);
		}

		@Override
		public void onFailure(Error error) {
			// TODO Auto-generated method stub
			mActivity.mHandler.sendEmptyMessage(UserInfoActivity.CANCEL_WAITTING);
			L.v(TAG,
					"LoginCallBack onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());
			
			if(error.isNetworkError()){
				FoneUtil.showToast(mActivity.getApplicationContext(), "网络超时,请稍后重试");
				return;
			}
			FoneUtil.showToast(mActivity.getApplicationContext(), "请求失败");
		}
	}
}
